from flask import Flask, render_template, request
import pandas as pd

app = Flask(__name__)

# 读取工资数据
file_path = "计件工资统计.xlsx"
df_wages = pd.read_excel(file_path, sheet_name="详细记录")

def get_employee_wage(name):
    """查询员工工资明细（支持多个职务）"""
    result = df_wages[df_wages["姓名"] == name]
    return result.to_dict(orient="records") if not result.empty else []

def calculate_total_wage(wages):
    """计算工资总额"""
    return sum(wage["计件工资金额"] for wage in wages) if wages else 0

@app.route('/', methods=['GET', 'POST'])
def index():
    wages = []
    employee_name = ""
    total_wage = 0
    if request.method == 'POST':
        employee_name = request.form.get("name")
        wages = get_employee_wage(employee_name)
        total_wage = calculate_total_wage(wages)
    return render_template("index.html", wages=wages, employee_name=employee_name, total_wage=total_wage)

if __name__ == '__main__':
    app.run(debug=True)
